package com.kehutong.charge.admin;

import com.kehutong.charge.entity.ChargeFeeChange;
import com.kehutong.charge.entity.ChargeFeeManage;
import com.kehutong.charge.entity.ChargeHouseConfig;
import com.kehutong.charge.service.ChargeFeeApplyService;
import com.kehutong.common.DeptService;
import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.util.time.TimeUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.Timestamp;
import java.util.*;

/**
 * @program: kht
 * @description: 费用申请
 * @author: jian Ye
 * @create: 2021/8/4
 **/

@MvcAction
@ReqMapping("/admin/pay/charge/fee/apply")
public class ChargeFeeManageController {

    @Inject
    ChargeFeeApplyService chargeFeeApplyService;
    @Inject
    DeptService deptService;


    @ReqMapping("/page")
    @HasPermission("pay:charge:fee:view")
    public Object page(JSONObject jsonObject) throws Exception {
        return chargeFeeApplyService.findPage(jsonObject);
    }


    @ReqMapping("/page/export")
    @HasPermission("pay:charge:fee:view")
    public void pageExport(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObject){
        chargeFeeApplyService.pageExport(req,resp,jsonObject);
    }

    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:fee:edit")
    @ReqMapping("/save")
    public Object save(ChargeFeeManage entity, Token token) throws Exception {
        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0){
            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            entity.setDepartmentNo(deptDTO.getDepartmentNo());
            entity.setDepartmentNos(deptDTO.getDepartmentNos());
            entity.setDepartmentName(deptDTO.getDepartmentName());
        }
        entity.setCreateName(token.getUsername());
        return chargeFeeApplyService.addApplyFee(entity);
    }

    @Bean(copy=true)
    @HasPermission("pay:charge:fee:edit")
    @ReqMapping("/update")
    public Object update(ChargeFeeManage entity,Token token) throws Exception {
        if(!Arrays.asList(entity.getApplyUserNo().split(",")).contains(token.getUuid())){
            return Result.error(123,"不是指定的审核人！");
        }
        entity.setUpdateName(token.getUsername());
        entity.setUpdateNo(token.getUuid());
        entity.setApplyTime(TimeUtil.timestamp());
        return chargeFeeApplyService.update(entity);
    }

    @ReqMapping("/eum/state")
    @HasPermission("pay:charge:fee:view")
    public Object enumState() {
        return chargeFeeApplyService.eumState();
    }

}
